﻿using Entity.Package;
using Services;
using System;
using System.Text;
using Tools;

namespace BLL.Business
{
    public class SearchUserInfo
    {
        /// <summary>
        /// 处理用户查询请求包
        /// </summary>
        /// <param name="search"></param>
        public static void GetGasUserInfo(PackRequestSearch search, CSocket.ClientThread client)
        {
            try
            {
                var reqData = new { userId = search.GasUserID.Trim() };

                var ser = IAgentPayment.GetAgentPaymentService();
                var result = ser.QueryUserInfo(reqData);

                var pkSearchReturn = new PackReturnSearchSuccess();
                pkSearchReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();

                if (result.Code == Entity.Service.ResultType.Success)
                {
                    pkSearchReturn.ResponseCode = ((int)ResponseCode.交易成功).ToString().PadLeft(3, '0');

                    var resData = result.Data;

                    pkSearchReturn.OwnFeeMoney = resData["userOwe"].ToString();
                    pkSearchReturn.GasUserID = resData["meterRealNum"].ToString();
                    pkSearchReturn.UserName = resData["userName"].ToString();
                    pkSearchReturn.Address = resData["userAddress"].ToString();
                    pkSearchReturn.ProtocolType = "0";
                    pkSearchReturn.LastYE = resData["userBalance"].ToString();
                    pkSearchReturn.UserProperties = "0";
                    pkSearchReturn.Memo = resData["meterType"].ToString();
                    pkSearchReturn.ExtraFee1 = "";
                    pkSearchReturn.Penalty = resData["znj"].ToString();
                    pkSearchReturn.LastMeterNumber = "";
                    pkSearchReturn.ThisMeterNumber = "";
                    pkSearchReturn.Aggregate = Convert.ToDecimal(resData["znj"]) + Convert.ToDecimal(resData["userOwe"]) > 0 ? (Convert.ToDecimal(resData["znj"]) + Convert.ToDecimal(resData["userOwe"])).ToString() : "0";
                    pkSearchReturn.CbRecordTimes = "0";

                    LogHelper.WriteInfoLog("查询成功,结果：" + pkSearchReturn.ToString());
                }
                else
                {
                    pkSearchReturn.ResponseCode = ((int)Common.GetNoSuccessCode(result.Code)).ToString().PadLeft(3, '0');
                }

                pkSearchReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkSearchReturn.ToString()).Length).ToString());
                byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkSearchReturn);
                if (client.Send(sendPackage))  //返回包发送成功，关闭链接
                    client.CloseSocket();

                LogHelper.WriteInfoLog(string.Format("{0}用户信息查询调用结束！响应码：{1}", search.GasUserID.Trim(), pkSearchReturn.ResponseCode));
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GetUserPaymentInfo方法-用户查询缴费信息出现异常，异常信息：" + ex.Message + ex.StackTrace, ex);
                PackFailReturn pkFailReturn = new PackFailReturn();
                pkFailReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3, '0');
                pkFailReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }
        }
    }
}
